<template>
    <div class="comp6-box boxss">
        <div class="titles">
            <img class="titles-img" src="@/assets/img/index/comp7-1.png" alt=""> 功能推荐</div>
        <div class="grid-container" ref="gridContainer">
            <!-- 左右切换按钮 -->
            <div class="nav-buttons" v-if="totalPages > 1">
                <button 
                    class="nav-btn prev" 
                    @click="prevPage" 
                    :disabled="currentPage === 1"
                    title="上一页"
                >
                    <i class="el-icon-arrow-left"></i>
                </button>
                <button 
                    class="nav-btn next" 
                    @click="nextPage" 
                    :disabled="currentPage === totalPages"
                    title="下一页"
                >
                    <i class="el-icon-arrow-right"></i>
                </button>
            </div>
            <div class="grid" :class="gridClass" v-if="list.length > 0">
                <div class="flex" v-for="(value, index) in currentPageItems" :key="value.id || index" @click="go(value)">
                    <div class="names-img">
                        <img :src="value.img" alt="" @error="handleImageError">
                    </div>
                    <div class="right">
                        <div class="names">{{ value.title }}</div>
                        <div class="tips two-lines">{{ value.des }}</div>
                    </div>
                </div>
            </div>
            
            <!-- 暂无数据状态 -->
            <div class="no-data-container" v-else-if="!loading">
                <div class="no-data">
                    <div class="no-data-icon">
                        <i class="el-icon-box"></i>
                    </div>
                    <div class="no-data-text">
                        <p class="no-data-title">暂无功能推荐</p>
                        <p class="no-data-desc">当前没有可显示的功能推荐内容</p>
                    </div>
                   
                </div>
            </div>
            
            <!-- 加载状态 -->
            <div class="loading-container" v-else>
                <div class="loading-spinner">
                    <i class="el-icon-loading"></i>
                    <p>正在加载功能推荐...</p>
                </div>
            </div>
            
            <!-- 分页圆点指示器 -->
            <div class="pagination-dots" v-if="totalPages > 1 && list.length > 0">
                <span 
                    v-for="(page, index) in totalPages" 
                    :key="index"
                    :class="['dot', { active: currentPage === index + 1 }]"
                    @click="goToPage(index + 1)"
                ></span>
            </div>
        </div>
    </div>
</template>
<script>
import { getList } from '@/api/IntroductionPartners/index'
export default({
    data () {
        return {
            formInline: {
                pageSize: 999,
                pageNum: 1,
                type: '4',
            },
            list: [],
            currentPage: 1,
            itemsPerPage: 8, // 默认每页显示8个项目 (4列 x 2行)
            containerWidth: 0,
            resizeTimer: null,
            loading: false
        }
    },
    computed: {
        // 根据容器宽度决定网格布局
        gridClass() {
            return this.containerWidth < 800 ? 'grid-3-cols' : 'grid-4-cols';
        },
        
        // 计算每页显示的项目数量
        calculatedItemsPerPage() {
            // 3列布局显示6个项目 (3x2), 4列布局显示8个项目 (4x2)
            return this.containerWidth < 800 ? 6 : 8;
        },
        // 总页数
        totalPages() {
            return Math.ceil(this.list.length / this.calculatedItemsPerPage);
        },
        
        // 当前页显示的项目
        currentPageItems() {
            const start = (this.currentPage - 1) * this.calculatedItemsPerPage;
            const end = start + this.calculatedItemsPerPage;
            return this.list.slice(start, end);
        },
       
    },
    
    watch: {
        // 监听容器宽度变化，重置到第一页
        gridClass(newVal, oldVal) {
            if (newVal !== oldVal) {
                this.currentPage = 1;
            }
        }
    },
    
    mounted() {
        this.getLists();
        this.initResizeObserver();
    },
    
    beforeDestroy() {
        if (this.resizeObserver) {
            this.resizeObserver.disconnect();
        }
        if (this.resizeTimer) {
            clearTimeout(this.resizeTimer);
        }
    },
    methods: {
        //获取列表
        async getLists() {
            try {
                this.loading = true;
                const res = await getList(this.formInline);
                if (res.code === 200) {
                    this.list = res.rows || [];
                    this.currentPage = 1;
                } else {
                    this.$message.warning(res.msg || '获取功能推荐失败');
                    this.list = [];
                }
            } catch (error) {
                console.error('获取功能推荐数据错误:', error);
                this.$message.error('网络连接失败，请检查网络连接');
                this.list = [];
            } finally {
                this.loading = false;
            }
        },
        
        // 初始化容器宽度监听
        initResizeObserver() {
            this.$nextTick(() => {
                this.updateContainerWidth();
                // 使用 ResizeObserver 监听容器大小变化
                if (window.ResizeObserver) {
                    this.resizeObserver = new ResizeObserver(this.handleResize);
                    this.resizeObserver.observe(this.$refs.gridContainer);
                } else {
                    // 降级方案：使用 window resize 事件
                    window.addEventListener('resize', this.handleWindowResize);
                }
            });
        },
        
        // 处理容器大小变化
        handleResize(entries) {
            if (this.resizeTimer) {
                clearTimeout(this.resizeTimer);
            }
            
            this.resizeTimer = setTimeout(() => {
                this.updateContainerWidth();
            }, 150); // 防抖处理
        },
        
        // 处理窗口大小变化（降级方案）
        handleWindowResize() {
            this.handleResize();
        },
        // 更新容器宽度
        updateContainerWidth() {
            if (this.$refs.gridContainer) {
                this.containerWidth = this.$refs.gridContainer.offsetWidth;
            }
        },
        
        // 跳转到指定页面
        goToPage(page) {
            if (page >= 1 && page <= this.totalPages) {
                this.currentPage = page;
            }
        },
        // 上一页
        prevPage() {
            if (this.currentPage > 1) {
                this.currentPage--;
            }
        },
        // 下一页
        nextPage() {
            if (this.currentPage < this.totalPages) {
                this.currentPage++;
            }
        },
        
        // 刷新数据
        refreshData() {
            this.getLists();
        },
        
        // 处理图片加载错误
        handleImageError(event) {
            // 设置默认图片或隐藏图片
            event.target.src = '';
            event.target.style.opacity = '0.5';
        },
         go(value) {
            if(this.checkPermi([value.permission]) || !value.permission) {
                this.$router.push({
                    path: '/'+value.jump
                });
            }else {
                this.$message.error('暂无该功能权限,请联系管理员开通!');

            }
        }
    }
})
</script>
<style lang="scss" scoped>
@import url('./style.css');

.grid-container {
    position: relative;
}

.comp6-box {
    &:hover {
        .nav-buttons {
            opacity: 1;
        }
    }
}
// 左右导航按钮样式
.nav-buttons {
    position: absolute;
    top: 50%;
    left: 0;
    right: 0;
    transform: translateY(-50%);
    pointer-events: none;
    z-index: 10;
     opacity: 0;
    .nav-btn {
        position: absolute;
        top: 0;
       
        width: 36px;
        height: 36px;
        border: 1px solid #e0e0e0;
        background: rgba(255, 255, 255, 0.95);
        border-radius: 50%;
        cursor: pointer;
        display: flex;
        align-items: center;
        justify-content: center;
        font-size: 16px;
        color: #666;
        transition: all 0.3s ease;
        pointer-events: auto;
        box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
        margin-top: -18px;
        &:hover:not(:disabled) {
            background: #6C5DD3;
            color: white;
            border-color: #6C5DD3;
            transform: scale(1.1);
        }
        
        &:disabled {
            opacity: 0.4;
            cursor: not-allowed;
            background: #f5f5f5;
        }
        
        &.prev {
            left: -14px;
        }
        
        &.next {
            right: -14px;
        }
    }
}

// 暂无数据样式
.no-data-container {
  
    display: flex;
    align-items: center;
    justify-content: center;
    padding: 20px 20px;
}

.no-data {
    text-align: center;
    max-width: 320px;
    
    .no-data-icon {
        margin-bottom: 16px;
        
        i {
            font-size: 64px;
            color: #d9d9d9;
        }
    }
    
    .no-data-text {
        margin-bottom: 24px;
        
        .no-data-title {
            font-size: 16px;
            color: #333;
            margin: 0 0 8px 0;
            font-weight: 500;
        }
        
        .no-data-desc {
            font-size: 14px;
            color: #999;
            margin: 0;
            line-height: 1.5;
        }
    }
    
    .no-data-action {
        .el-button {
            padding: 8px 20px;
        }
    }
}

// 加载状态样式
.loading-container {
    min-height: 200px;
    display: flex;
    align-items: center;
    justify-content: center;
    padding: 40px 20px;
}

.loading-spinner {
    text-align: center;
    color: #999;
    
    i {
        font-size: 32px;
        margin-bottom: 12px;
        display: block;
        animation: rotating 1s linear infinite;
    }
    
    p {
        margin: 0;
        font-size: 14px;
    }
}

@keyframes rotating {
    from {
        transform: rotate(0deg);
    }
    to {
        transform: rotate(360deg);
    }
}

.grid {
    border-radius: 10px;
    margin-top: 6px;
    padding: 8px 8px 0px 8px;
    display: grid;
    gap: 14px;
    min-height: 163px; // 确保有足够的高度
    
    &.grid-3-cols {
        grid-template-columns: repeat(3, 1fr);
        grid-template-rows: repeat(2, 1fr);
    }
    
    &.grid-4-cols {
        grid-template-columns: repeat(4, 1fr);
        grid-template-rows: repeat(2, 1fr);
    }
}

// 分页圆点样式
.pagination-dots {
    display: flex;
    justify-content: center;
    align-items: center;
    padding: 6px 0;
    gap: 8px;
    
    .dot {
        width: 8px;
        height: 8px;
        border-radius: 50%;
        background-color: #d9d9d9;
        cursor: pointer;
        transition: all 0.3s ease;
        
        &:hover {
            background-color: #999;
            transform: scale(1.2);
        }
        
        &.active {
            background-color: #6C5DD3;
            width: 12px;
            height: 12px;
        }
    }
}

.names-img {
    width: 58px;
    height: 56px;
    flex-shrink: 0;
    img {
        width: 100%;
        height: 100%;
        border-radius: 4px;
        transition: opacity 0.3s ease;
    }
}

.flex {
    cursor: pointer;
    padding: 8px;
    background: #FAFAFF;
    border-radius: 6px;
    transition: all 0.2s ease;
    display: flex;
    align-items: center;
        &:hover {
        background: #f0f0ff;
        transform: translateY(-2px);
        box-shadow: 0 4px 12px rgba(108, 93, 211, 0.1);
    }
}

.names {
    font-size: 14px;
    font-weight: 500;
    margin-bottom: 4px;
    color: #333;
    line-height: 1.4;
}

.tips {
    font-size: 12px;
    color: #8F92A1;
    line-height: 1.4;
    
    &.two-lines {
        display: -webkit-box;
        -webkit-line-clamp: 2;
        -webkit-box-orient: vertical;
        overflow: hidden;
        text-overflow: ellipsis;
    }
}

.titles-img {
    width: 20px;
}

.right {
    margin-left: 12px;
    flex: 1;
    min-width: 0; // 确保文本能正确截断
}

// 响应式设计
@media (max-width: 768px) {
    .nav-buttons {
        .nav-btn {
            width: 32px;
            height: 32px;
            font-size: 14px;
            
            &.prev {
                left: -16px;
            }
            
            &.next {
                right: -16px;
            }
        }
    }
    
    .grid {
        &.grid-4-cols {
            grid-template-columns: repeat(2, 1fr);
            grid-template-rows: repeat(4, 1fr);
        }
        
        &.grid-3-cols {
            grid-template-columns: repeat(2, 1fr);
            grid-template-rows: repeat(3, 1fr);
        }
    }
    
    .flex {
        padding: 6px;
    }
    
    .names {
        font-size: 13px;
    }
    
    .tips {
        font-size: 11px;
    }
    
    .no-data {
        .no-data-icon i {
            font-size: 48px;
        }
        
        .no-data-text {
            .no-data-title {
                font-size: 15px;
            }
            
            .no-data-desc {
                font-size: 13px;
            }
        }
    }
}

@media (max-width: 480px) {
    .nav-buttons {
        .nav-btn {
            width: 28px;
            height: 28px;
            font-size: 12px;
            
            &.prev {
                left: -14px;
            }
            
            &.next {
                right: -14px;
            }
        }
    }
    
    .grid {
        &.grid-4-cols,
        &.grid-3-cols {
            grid-template-columns: 1fr;
            grid-template-rows: repeat(8, 1fr);
        }
        
        gap: 12px;
        padding: 6px;
    }
    
    .pagination-dots {
        padding: 12px 0;
        
        .dot {
            width: 6px;
            height: 6px;
            
            &.active {
                width: 10px;
                height: 10px;
            }
        }
    }
    
    .no-data-container {
        min-height: 250px;
        padding: 30px 15px;
    }
}
</style>